package com.ruoyi.api.controller.deal;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.api.annotation.CurrentUser;
import com.ruoyi.api.annotation.IgnoreAuth;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.deal.bo.HyDealGoodsAddBo;
import com.ruoyi.deal.bo.HyDealGoodsQueryBo;
import com.ruoyi.deal.service.IHyDealGoodsService;
import com.ruoyi.deal.vo.HyDealGoodsVo;
import com.ruoyi.system.domain.HyUser;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品api接口
 * @author lixin
 * @date 2021-06-18 20:57
 */
@RestController
@RequestMapping("/api/goods")
public class ApiGoodsController extends BaseController {

	@Autowired
	private IHyDealGoodsService dealGoodsService;

	/**
	 * 查询商品列表
	 */
	@IgnoreAuth
	@GetMapping("/list")
	public TableDataInfo<HyDealGoodsVo> list(HyDealGoodsQueryBo bo) {
		if (ObjectUtil.isNull(bo.getStatus())) {
			bo.setStatus(1);
		}
		return dealGoodsService.queryPageList(bo);
	}

	@GetMapping("/myList")
	public AjaxResult list(@CurrentUser HyUser user, HyDealGoodsQueryBo bo) {
		bo.setUserId(user.getUserId().intValue());
		List<HyDealGoodsVo> list = dealGoodsService.queryList(bo);
		return AjaxResult.success(list);
	}

	/**
	 * 获取商品管理详细信息
	 */
	@IgnoreAuth
	@GetMapping("/{goodsId}")
	public AjaxResult<HyDealGoodsVo> getInfo(@PathVariable("goodsId") Integer goodsId) {
		return AjaxResult.success(dealGoodsService.queryById(goodsId));
	}

	/**
	 * 商品发布
	 * @param user
	 * @param bo 商品信息
	 * @return
	 */
	@PostMapping("/publish")
	public AjaxResult publish(@CurrentUser HyUser user, @RequestBody HyDealGoodsAddBo bo) {
		if (StrUtil.isEmpty(user.getQqnumber())) {
			return AjaxResult.error("个人信息不完善，请在个人信息中填写联系QQ");
		}
		bo.setUserId(user.getUserId().intValue());

		Map<String, Object> resultObj = new HashMap<String, Object>(1);
		resultObj.put("goodsId", dealGoodsService.publish(bo));

		return AjaxResult.success(resultObj);
	}


	/**
	 * 下架商品
	 * @param goodsId
	 * @return
	 */
	@PutMapping("/soldOut/{goodsId}")
	public AjaxResult soldOut(@PathVariable("goodsId") Integer goodsId) {

		if(dealGoodsService.soldOutByGoodsId(goodsId)) {
			return AjaxResult.success("下架成功！");
		} else {
			return AjaxResult.error("下架失败！");
		}

	}

	/**
	 * 上架商品
	 * @param goodsId
	 * @return
	 */
	@PutMapping("/putaway/{goodsId}")
	public AjaxResult putaway(@PathVariable("goodsId") Integer goodsId) {

		if(dealGoodsService.putawayByGoodsId(goodsId)) {
			return AjaxResult.success();
		} else {
			return AjaxResult.error();
		}

	}

	/**
	 * 删除商品
	 * @param goodsId
	 * @return
	 */
	@DeleteMapping("/delete/{goodsId}")
	public AjaxResult delete(@PathVariable("goodsId") Integer goodsId) {

		if(dealGoodsService.deleteByGoodsId(goodsId)) {
			return AjaxResult.success("删除成功！");
		} else {
			return AjaxResult.error("删除失败！");
		}

	}


	/**
	 * 获取商品所属用户
	 */
	@IgnoreAuth
	@GetMapping("/user/{goodsId}")
	public AjaxResult<HyUser> getUserInfo(@PathVariable("goodsId") Integer goodsId) {
		HyUser user = dealGoodsService.queryUserByGoodsId(goodsId);
		return AjaxResult.success(user);
	}

}
